home *** CD-ROM | disk | FTP | other *** search
/ AppleScript - The Beta Release / AppleScript - The Beta Release.iso / Documentation / develop / Apple Event Objects and You / Apple Event Objects (code) / ErrorHandlers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-08  |  5.4 KB  |  218 lines  |  [TEXT/KAHL]

  1. #include <string.h>
  2.  
  3. #include "ScriptScrap.h"
  4. #include "Prototypes.h"
  5.  
  6.  
  7. /* Constants for the message strings which appear in our error dialog */
  8. const short kUrgent = 3001;
  9. const short kIn = 3002;
  10.  
  11. /* Constants for our message dialogs */
  12. const short kFatalErr = 2003;
  13. const short kWarning  = 2004;
  14. const short kNotice   = 2005;
  15.  
  16. typedef struct {
  17.     short    indices[100];
  18.     char    text[2];    /* Pascal-style error strings begin here */
  19. } ErrorStrings, *ErrorStringsPtr, **ErrorStringsHand;
  20.  
  21. /* Prototypes for private routines */
  22.  
  23. static    void    _ErrorDialog(char *errorMsg, char *location, Boolean isFatal);
  24. static    short    _CheckPointer(void *p);
  25.  
  26. static    void _ErrorDialog (char *errorMsg, char *location, Boolean isFatal)
  27. {
  28. enum {
  29.         kQuit = 1,
  30.         kDebugger,
  31.         kContinue
  32.      };
  33.  
  34.     char            inParam[4];
  35.     short            result;
  36.     StringHandle    str_hndl;
  37.  
  38.     CtoPstr(errorMsg); CtoPstr(location);
  39.     if (location[0] == 0)
  40.         ParamText((StringPtr)errorMsg, (void*)"", (StringPtr)location, (void*)"");
  41.     else {
  42.         str_hndl = GetString ( kIn );
  43.         if ( str_hndl )
  44.         {
  45.             HLock ( (void *)str_hndl );
  46.                 ParamText((StringPtr)errorMsg, (StringPtr)*str_hndl, (StringPtr)location, (void*)"");
  47.             HUnlock ( (void *)str_hndl );
  48.             ReleaseResource ( (void *)str_hndl );
  49.         }
  50.     }
  51.     PtoCstr((unsigned char *)errorMsg); PtoCstr((unsigned char *)location);
  52.     MyInteractWithUser(true);    /* Call the notification manager & make sure we're in front */
  53.     if (isFatal)
  54.         result = StopAlert(kFatalErr, NULL);
  55.     else
  56.         result = CautionAlert(kWarning, NULL);
  57.     if (result == kDebugger)
  58.         Debugger();
  59.     if (isFatal || (result == kQuit))
  60.         ExitToShell();
  61. } /* _ErrorDialog */
  62.  
  63.  
  64. static    short _CheckPointer (void *p)
  65. {
  66.     if (p == NULL)
  67.         return kPointerNULL;
  68.  
  69.     if ((long)p & 0x00000001)
  70.         return kPointerOdd;
  71.  
  72.     return kPointerOK;
  73. } /* _CheckPointer */
  74.  
  75.  
  76. void Assert (Boolean condition, char *message, char *location, Boolean isFatal)
  77.   /* Notify the user if the specified condition isn't true. If isFatal is TRUE, */
  78.   /* exit to the Finder afterwards */
  79. {
  80.     char    messageCopy[256];
  81.  
  82.     if (!condition) {
  83.         strcpy(messageCopy, "Assertion failed: ");
  84.         strcat(messageCopy, message);
  85.         _ErrorDialog(messageCopy, location, isFatal);
  86.     }
  87. } /* Assert */
  88.  
  89.  
  90. Boolean AssertOK (Boolean condition, char *message, char *location)
  91. /* Notify the user if the specified condition isn't true, and return "condition" */
  92. /* for use in an if statement. */
  93. {
  94.     Assert(condition, message, location, FALSE);
  95.     return condition;
  96. } /* AssertOK */
  97.  
  98.  
  99. void CheckPointer (void *p, char *ptrName, char *location)
  100. {
  101.     char    errorMsg[256];
  102.  
  103.     strcpy(errorMsg, ptrName);
  104.     switch (_CheckPointer(p)) {
  105.         case kPointerOK:
  106.             return;
  107.  
  108.         case kPointerNULL:
  109.             strncat(errorMsg, " is NULL", 255);
  110.             break;
  111.  
  112.         case kPointerOdd:
  113.             strncat(errorMsg, " is odd", 255);
  114.             break;
  115.  
  116.     };
  117.     _ErrorDialog(errorMsg, location, kIsFatal);
  118. } /* CheckPointer */
  119.  
  120.  
  121. void CheckHandle (void *h, char *handleName, char *location)
  122. {
  123.     char    place_holder[257];
  124.  
  125.     strcpy( place_holder, "*" );
  126.     strcat( place_holder, handleName );
  127.     CheckPointer(h, place_holder, location);
  128.     strcpy( place_holder, "**" );
  129.     strcat( place_holder, handleName );
  130.     CheckPointer(*((Handle)h), place_holder, location);
  131. } /* CheckHandle */
  132.  
  133.  
  134.  
  135. void CheckResult (short code, char *location)
  136. {
  137.     Str255                errorMsg, errorNum;
  138.     ErrorStringsHand    messageRsrc;
  139.     long                messageOffset;
  140.     Str255                message;
  141.     short                rsrcNum;
  142.  
  143.     if (code) {
  144.         NumToString(code, errorNum);
  145.         PtoCstr((unsigned char *)errorNum);
  146.         strcpy((char *)errorMsg, "Error code ");
  147.         strcat((char *)errorMsg, (char *)errorNum);
  148.  
  149.         /* Now, append the error's name to our message */
  150.         strcpy((char *)message, "Unknown");
  151.         rsrcNum = code / 100;
  152.         /* if the error number is negative, then the resource ID should be negative */
  153.         /* If the error number is positive, we make sure the resource ID is > 0 (to */
  154.         /* avoid confusion as to whether 0 should be used for positive or negative    */
  155.         /* error codes.                                                                */
  156.         if (rsrcNum > 0)
  157.             ++rsrcNum;
  158.         else
  159.             --rsrcNum;
  160.  
  161.         messageRsrc = (ErrorStringsHand)GetResource('ERRS', rsrcNum);    /* Each error string resource holds a range of 100 error numbers */
  162.         if (messageRsrc != NULL) {
  163.             if (code < 0)
  164.                 code = - code;
  165.             messageOffset =(*messageRsrc)->indices[code % 100] - (100 * sizeof(short));
  166.             if (messageOffset != 0) {
  167.                 BlockMove(&((*messageRsrc)->text[messageOffset]), message, (*messageRsrc)->text[messageOffset] + 1);
  168.                 PtoCstr(message);
  169.             }
  170.         }
  171.  
  172.         strcat((char *)errorMsg, " (");
  173.         strcat((char *)errorMsg, (char *)message);
  174.         strcat((char *)errorMsg, ")");
  175.         _ErrorDialog((char*)errorMsg, location, kIsFatal);
  176.     };
  177. } /* CheckResult */
  178.  
  179.  
  180. void Error (char *errorMsg, char *location)
  181. {
  182.     _ErrorDialog(errorMsg, location, kIsFatal);
  183. } /* Error */
  184.  
  185.  
  186. Boolean IsValidPointer (void *p)
  187. {
  188.     return _CheckPointer(p) == kPointerOK;
  189. } /* IsValidPointer */
  190.  
  191.  
  192. Boolean IsValidHandle (void *h)
  193. {
  194.     return IsValidPointer(h) && IsValidPointer(*(void **)h);
  195. } /* IsValidHandle */
  196.  
  197.  
  198. void ShowMessageAndDie (char *message)
  199. /* This routine displays an error message and then aborts back to the main event loop */
  200. {
  201.     _ErrorDialog(message, "", kNonFatal);
  202. } /* ShowMessageAndDie */
  203.  
  204.  
  205. void ShowMessage (char *message)
  206. /* This routine displays an error message and then continues execution */
  207. {
  208.     short itemHit;
  209.  
  210.     CtoPstr(message);
  211.     ParamText((StringPtr)message, (void*)"", (void*)"", (void*)"");
  212.     PtoCstr((unsigned char *)message);
  213.     MyInteractWithUser(true);    /* Call the notification manager & make sure we're in front */
  214.     itemHit = NoteAlert(kNotice, NULL);
  215.     if (itemHit == 1)
  216.         ExitToShell();
  217. } /* ShowMessage */
  218.